<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="角色" prop="role">
        <el-select v-model="queryParams.role" placeholder="请选择角色" clearable style="width: 120px">
          <el-option label="售前" value="售前" />
          <el-option label="售中" value="售中" />
          <el-option label="售后" value="售后" />
          <el-option label="技术" value="技术" />
          <el-option label="售前主管" value="售前主管" />
          <el-option label="售中主管" value="售中主管" />
          <el-option label="售后主管" value="售后主管" />
          <el-option label="技术主管" value="技术主管" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:task:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="考核任务Id" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="角色" align="center" prop="role" />
      <el-table-column label="细节" align="center" prop="description" />
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <el-tag :type="scope.row.state === 0 ? 'success' : 'info'">{{ scope.row.state === 0 ? '启用' : '禁用' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="添加人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-switch-button"
            @click="toggleState(scope.row)"
            v-hasPermi="['system:task:edit']"
          >{{ scope.row.state === 0 ? '禁用' : '启用' }}</el-button>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加绩效考核任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select v-model="form.role" placeholder="请选择角色" style="width: 100%">
            <el-option label="售前" value="售前" />
            <el-option label="售中" value="售中" />
            <el-option label="售后" value="售后" />
            <el-option label="技术" value="技术" />
            <el-option label="售前主管" value="售前主管" />
            <el-option label="售中主管" value="售中主管" />
            <el-option label="售后主管" value="售后主管" />
            <el-option label="技术主管" value="技术主管" />
          </el-select>
        </el-form-item>
        <el-form-item label="细节" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入细节" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTask, addTask, updateTask } from "@/api/system/assess"

export default {
  name: "assess",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 绩效考核任务信息表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ],
        role: [
          { required: true, message: '角色不能为空', trigger: 'change' }
        ],
        description: [
          { required: true, message: '细节不能为空', trigger: 'blur' }
        ]
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        role: null,
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询绩效考核任务信息列表 */
    getList() {
      this.loading = true
      listTask(this.queryParams).then(response => {
        this.taskList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.queryParams.role = null
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
    },
    // 新增
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加绩效考核任务信息"
    },
    // 切换启用禁用
    toggleState(row) {
      const newState = row.state === 0 ? 1 : 0
      const payload = { ...row, state: newState }
      updateTask(payload).then(() => {
        this.$modal.msgSuccess(newState === '0' ? '已启用' : '已禁用')
        this.getList()
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        role: null,
        description: null,
        state: '0'
      }
      this.resetForm("form")
    },
    // 提交按钮
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addTask(this.form).then(() => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
      })
    }
  }
}
</script>
